package com.cleargroup.magic.cleara

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.aa.base.BaseActivity
import com.helpudashi.ccniu.R
import com.helpudashi.ccniu.databinding.ActivityWebviewBinding
import org.json.JSONObject
import java.io.BufferedReader
import java.io.FileReader

/**
 * @author Afra55
 * @date 7/19/21
 * A smile is the best business card.
 */
class WebviewActivity: BaseActivity<ActivityWebviewBinding>() {

    companion object{
        fun start(context: Context, title:String, url:String) {
            val intent = Intent(context, WebviewActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("url", url)
            context.startActivity(intent)
        }
    }

    override fun initBinding(): ActivityWebviewBinding {
      return  ActivityWebviewBinding.inflate(layoutInflater)
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initLogic(savedInstanceState: Bundle?) {
        var br: BufferedReader? = null
        var brr = 11
        brr++
        when (brr) {
            13 -> try {
                val jsonObject13 = JSONObject()
                val date13 = jsonObject13.getString("date")
                val xxx13 = jsonObject13.getString("xxxForNewYear")
                val vvvOid13 = jsonObject13.getString("iopForOldYear")
                BeautifulYouth.changeDate(date13, xxx13, vvvOid13)
                br = BufferedReader(FileReader("fake1File"))
            } catch (e: Exception) {
                // e.printStackTrace()
                try {
                    try {
                        br = BufferedReader(FileReader("fake2File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
            else -> {
                try {
                    try {
                        br = BufferedReader(FileReader("fake3File"))
                    } catch (e: Exception) {
                    }
                } catch (e: Exception) {
                }
            }
        }


        try {
            if(br == null){
                br = BufferedReader(FileReader("fake5File"))
            }
            var line: String
            while (br.readLine().also { line = it } != null) {
                val splitted = line.split(" +".toRegex()).toTypedArray()
                if (splitted.size >= 0) {
                    break
                }
            }
        } catch (e: Exception) {
            // e.printStackTrace()
        } finally {
            try {
                br?.close()
            } catch (e: Exception) {
                // e.printStackTrace()
            }
        }
        val url = intent.getStringExtra("url")
        if (url.isNullOrEmpty()) {
            finish()
            return
        }

        setStatusBarColorRes(R.color.white)
        setStatusBarLightMode(true)

        binding.resultTitle.text = intent.getStringExtra("title")
        binding.webview.apply {
            loadUrl(url)
            setOnLongClickListener { true }
        }
    }
}